using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;
using Barbar.MapReport.Common;
using Barbar.MapReport.Common.Drawing;
using Barbar.MapReport.Common.Drawing.Commands;
using Barbar.MapReport.Configuration;
using Barbar.MapReport.DataSource.Facade;
using Barbar.MapReport.DataSource.Query;
using Barbar.MapReport.HtmlRender;
using System.Drawing;
using Barbar.MapReport.HtmlRender.Json;
using Barbar.MapReport.DataSource;

public partial class CampaignCostsReport : MapReportDemoPage {
  protected void Page_Load(object sender, EventArgs e) {
    Polyline polyline = new Polyline(new Barbar.MapReport.DataSource.Point[] {
      new Barbar.MapReport.DataSource.Point(10m, 5m),
      new Barbar.MapReport.DataSource.Point(20m, 20m)
    });
    PolylineFactory factory = new PolylineFactory(polyline, Color.Red, 5);
    string value = factory.ToJson();
    
    SortedList<string, BusinessRegionList> regions = new SortedList<string, BusinessRegionList>();

    IDataSourceService service = new DataSourceService();
    foreach (Contact contact in DataProvider.Instance.Contacts) {
      if (!contact.Latitude.HasValue || !contact.Longitude.HasValue)
        continue;
      GeoPoint point = new GeoPoint(contact.Latitude.Value, contact.Longitude.Value);
      RegionRequestQuery query = new RegionRequestQuery();
      query.Criteria = new RegionContainsPoint(point);
      MultipleRegionResponse response = (MultipleRegionResponse)service.GetRegions(query);
      if (response.Regions != null && response.Regions.Count > 0) {
        // we ignore more results - just take the first region
        string regionId = response.Regions[0].Id;
        BusinessRegionList region;
        if (!regions.TryGetValue(regionId, out region)) {
          region = new BusinessRegionList();
          region.ToolTip = String.Format(CultureInfo.InvariantCulture, "Campaign costs for<br/>{0}: {{0}} EUR", response.Regions[0].Name);
          region.Value = 1.0m;
          region.RegionIds.Add(regionId);
          regions.Add(regionId, region);
        } else {
          region.Value += 1.0m;
        }
      }
    }

    AddRegionsWithBusinessProcessingCommand cmd = new AddRegionsWithBusinessProcessingCommand();
    foreach (BusinessRegionList valueRegion in regions.Values) {
      valueRegion.Value *= DataProvider.Instance.CampaignCost / DataProvider.Instance.Contacts.Count;
      valueRegion.ToolTip = String.Format(CultureInfo.InvariantCulture, valueRegion.ToolTip, (int)valueRegion.Value);
      cmd.Regions.Add(valueRegion);
    }

    // was USD before crisis :)
    cmd.LegendDescription = new LegendDescription(Color.Red, "Campaign costs", "EUR");

    cmd.RegionOptions = new RegionOptions();
    cmd.RegionOptions.PolygonBehavior = new PolygonBehaviorBubble("{Content}");
    cmd.RegionOptions.CategoriesCount = 5;
    cmd.RegionOptions.Opacity = 0.5f;
        
    RenderingService renderService = new RenderingService();
    renderService.Execute(cmd);

    RenderSettings settings = new RenderSettings(new SerializedUnit(800), new SerializedUnit(600), 51, 11, 6, MapConfigurationManager.MapSettings.GoogleKeyProvider.DefaultGoogleKey, 2057);
    settings.EnableGoogleBar = true;

    MapHtmlFragment fragment = renderService.Render(settings);

    litMapDiv.Text = fragment.MapElement + fragment.LegendElements[0];
  }
}
